local xiaoji = fk.CreateSkill {

  name = "joy_mou__xiaoji",

  tags = {  },

}



xiaoji:addEffect(fk.AfterCardsMove, {
  name = "joy_mou__xiaoji",
  anim_type = "drawcard",
  events = {fk.AfterCardsMove},
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(xiaoji.name) then return end
    for _, move in ipairs(data) do
      if move.from==player then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerEquip then
            return true
          end
        end
      end
    end
  end,
  on_trigger = function(self, event, target, player, data)
    local i = 0
    for _, move in ipairs(data) do
      if move.from==player then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerEquip then
            i = i + 1
          end
        end
      end
    end
    self.cancel_cost = false
    for _ = 1, i do
      if self.cancel_cost or not player:hasSkill(xiaoji.name) then break end
      self:doCost(event, target, player, data)
    end
  end,
  on_cost = function(self, event, target, player, data)
    if player.room:askForSkillInvoke(player, xiaoji.name) then
      return true
    end
    self.cancel_cost = true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:drawCards(player, 2, xiaoji.name)
    if player.dead then return false end
    local targets = table.map(table.filter(room.alive_players, function (p)
      return #p:getCardIds("ej") > 0
    end), Util.IdMapper)
    if #targets == 0 then return false end
    local tos = room:askForChoosePlayers(player, targets, 1, 1, "#joy_mou__xiaoji-discard", xiaoji.name, true)
    if #tos == 0 then return false end
    local to = room:getPlayerById(tos[1])
    local card = room:askForCardChosen(player, to, "ej", xiaoji.name)
    room:throwCard({card}, xiaoji.name, to, player)
  end,
})

return xiaoji